/*
 * Conference Guru
 * ConferenceViewPanel.java
 * version Autumn 2012
 * 
 * by Lawrence Grass
 */
package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

import model.Conference;
import model.Portal;

@SuppressWarnings("serial")
public class ConferenceViewPanel extends JPanel
						  implements Observer, ActionListener
{
	
	final int SIDEBAR_WIDTH = 135;
	
	final int SIDEBAR_HEIGHT = 640;
	
	final String my_user_name;
	
	final Portal my_portal;
	
	final BaseViewFrame my_main_frame;
	
	JButton my_add_conf = new JButton("Add a Conference");
	
	ArrayList<JButton> my_conf_list = new ArrayList<JButton>();
	
	WorkspaceViewPanel my_workspace;
	
	JButton my_back_button;
	
	JButton my_logout_button;
	
	JPanel my_base_panel;
	
	JScrollPane my_outer_scroll_pane = new JScrollPane();
	
	/*
	 * Creates a ConferenceViewPanel which will display all the conferences available and let you choose
	 * as well as add a conference.
	 */
	public ConferenceViewPanel(Portal the_portal, String the_user_id, BaseViewFrame main_frame)
	{		
		super(new BorderLayout());
		my_user_name = the_user_id;
		my_portal = the_portal;
		my_portal.addObserver(this);	
		my_main_frame = main_frame;
		my_base_panel = new JPanel(new BorderLayout());
		my_base_panel.setOpaque(false);
		
		this.setBackground(BaseViewFrame.BACKGROUND_COLOR);
		backButtonSetup();
		logoutButtonSetup();
		sideBarSetup();
		viewPaneSetup();
		titlePaneSetup();
		add(my_base_panel, BorderLayout.CENTER);
		
		
	}
	
	
	private void logoutButtonSetup() {
		my_logout_button = new JButton("Logout");	
		my_logout_button.addActionListener(this);
		
	}

	private void backButtonSetup() {
		my_back_button = new JButton("Back to Conference List");
		my_back_button.addActionListener(this);
		
	}

	/*
	 * The method that sets up the sidebar menu (west panel)
	 */
	private void sideBarSetup() {
		
		my_add_conf.setFocusPainted(false); //removes the outline around the text
		my_add_conf.addActionListener(this);
		
		JPanel side_menu = new JPanel();
		side_menu.setLayout(new BoxLayout(side_menu, BoxLayout.Y_AXIS));
		side_menu.setOpaque(false);
		my_logout_button.setMaximumSize(my_add_conf.getPreferredSize());
		side_menu.add(my_logout_button);
		side_menu.add(my_add_conf);
		side_menu.setPreferredSize(new Dimension(SIDEBAR_WIDTH, SIDEBAR_HEIGHT));
		my_base_panel.add(side_menu, BorderLayout.WEST);
		
	}

	/*
	 * The method that sets up the Title area (north panel).
	 */
	private void titlePaneSetup() {
		JPanel title_pane = new JPanel();
		title_pane.setOpaque(false);
		title_pane.setPreferredSize(new Dimension(800, 200));
		JLabel welcome = new JLabel("Welcome to the Conference Guru, " + my_user_name + "!");
		welcome.setFont(new Font("serif", Font.ROMAN_BASELINE, 30));
		welcome.setForeground(Color.BLACK);
		title_pane.add(welcome);
		my_base_panel.add(title_pane, BorderLayout.NORTH);
	}

	/*
	 * A method that sets up the main view display that lists all the conferences.
	 */
	private void viewPaneSetup()
	{		
		JPanel outer_view_panel = new JPanel(); 
		outer_view_panel.setLayout(new BoxLayout(outer_view_panel, BoxLayout.Y_AXIS));
		outer_view_panel.setBackground(BaseViewFrame.BACKGROUND_COLOR);
		
		//Goes through and displays each conference.
		for(Conference conf: my_portal.getAllConferences())
		{	
			JPanel conf_panel = new JPanel(new BorderLayout());	
			conf_panel.setMaximumSize(new Dimension(600, 60));
					
			JButton conf_button = new JButton("Title: " + conf.getConfTitle());
			conf_button.addActionListener(this);
			my_conf_list.add(conf_button);
			conf_panel.add(conf_button, BorderLayout.NORTH);
			
			//gives us who the program chair is
			JPanel conf_info = new JPanel(new GridLayout(2, 2));
			conf_info.setBorder(BorderFactory.createEtchedBorder());
			conf_info.setBackground(BaseViewFrame.BACKGROUND_COLOR);
			JLabel pc_title = new JLabel("The Program Chair: ");
			conf_info.add(pc_title);
			JLabel pc_name = new JLabel(conf.getProgChairUserName());
			conf_info.add(pc_name);
			
			//tells us the dates
			JLabel deadline_title = new JLabel("Submission Deadline: ");
			conf_info.add(deadline_title);
			JLabel deadline = new JLabel(conf.getDeadline());
			conf_info.add(deadline);
			conf_panel.add(conf_info);
			
			outer_view_panel.add(conf_panel);
			outer_view_panel.add(Box.createVerticalStrut(5));		
		}
		
		JScrollPane outer_scroll_pane = new JScrollPane(outer_view_panel);
		outer_scroll_pane.setBorder(BorderFactory.createEmptyBorder());
		my_base_panel.add(outer_scroll_pane, BorderLayout.CENTER);	
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		//Builds the add a conference dialog box and shows it.
		if(e.getSource()==my_add_conf)
		{
			JTextField title = new JTextField();	
			JFormattedTextField deadline;
			try {
				deadline = new JFormattedTextField(new MaskFormatter("##/##/####"));
				JComponent[] fields = new JComponent[] { new JLabel("Title: "),
						title, new JLabel("Date of deadline: "), deadline };
			
				JOptionPane.showMessageDialog(null, fields, "Conference Information",
												JOptionPane.PLAIN_MESSAGE);
			
				if (!title.getText().equals("") && !deadline.getText().equals("  /  /    "))
				{
					Conference temp = new Conference( title.getText(), my_user_name);
					temp.setDeadline(deadline.getText());
					my_portal.addConference(temp);
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Conference not created. A conference must have a Title and a Deadline", 
												"Empty fields.", JOptionPane.PLAIN_MESSAGE);
				}
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
		}
		//Registers the click on one of the confrence buttons and starts up that conferences WorkspaceView.
		else if (my_conf_list.contains(e.getSource()))
		{
			JButton button = (JButton) e.getSource();
			String[] parsed = button.getText().split(": ");
			String conf_title = parsed[1];
			Conference a_conf = my_portal.getConference(conf_title);
			my_workspace = new WorkspaceViewPanel(my_back_button, my_user_name, a_conf);

			my_main_frame.setContentPane(my_workspace);
			my_main_frame.validate();
			
		}
		//The actions taken if the back button is clicked
		else if (e.getSource() == my_back_button)
		{
			my_main_frame.setBackground(BaseViewFrame.BACKGROUND_COLOR);
			my_base_panel.setOpaque(false);
			my_main_frame.setContentPane(my_base_panel);
			this.repaint();
		}
		//logout
		else if (e.getSource() == my_logout_button)
		{
			my_main_frame.logout();
		}
		
	}

	@Override
	public void update(Observable o, Object arg) {
		viewPaneSetup();
		my_main_frame.login();		
	}
	
}
